<template>
  <div class="login">
    <!-- 左边 -->
    <div class="left-box">
      <!-- 右上角 logo -->


      <!-- 背景图 -->
      <img class="bg-img" src="../assets/image/login_left_tree.jpg" alt="tree" />

      <!-- 欢迎文字 -->
      <div class="welcome-text">
<!--        <h1 class="hi">HI，</h1>-->
<!--        <h1 class="welcome">荷叶木本</h1>-->
        <h1 class="brand">荷叶木本后台管理系统</h1>
      </div>

      <!-- 底部公司名 -->
      <div class="company-name">禾页木本教育咨询有限公司</div>
      <div class="icp">闽ICP备2025115978号-1</div>
    </div>

    <!-- 右边：登录框 -->
    <!-- 右边：登录框 -->
    <div class="right-box">
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" @keyup.enter.native="handleLogin">
      <img class="logo" src="../assets/logo/logo_avatar.png" alt="logo" />
      <div class="login-card">
        <div class="title-row">
          <h2 class="title">账号登录</h2>
          <img class="title-icon" src="../assets/icon/icon_about_us.png" alt="about" />
        </div>

        <!-- 用户名 -->
        <el-input v-model="loginForm.phonenumber" type="text" placeholder="用户名" class="underline-input"></el-input>
        <!-- 密码 -->
        <el-input v-model="loginForm.password" type="password" placeholder="密码" show-password class="underline-input"></el-input>
        <!-- 登录按钮 -->
        <el-button type="primary" class="login-btn" @click.native.prevent="handleLogin">登录</el-button>

        <!-- 其他登录方式 -->
        <div class="other-login">
          <span class="other-title">其他登录方式</span>
          <div class="icons">
            <div class="icon-item" @click="handleSmsLogin">
              <div class="circle">
                <img src="../assets/icon/message.png" alt="短信登录" />
              </div>
              <p>短信登录</p>
            </div>
          </div>
        </div>
      </div>
      </el-form>
    </div>
  </div>
</template>



<style lang="scss" scoped>
html,
body,
#app {
  height: 100%;
  margin: 0;
  padding: 0;
}

.login {
  display: flex;
  height: 100vh;
  width: 100vw;

  /* 左边区域 */
  .left-box {
    flex: 2;
    position: relative;
    overflow: hidden;

    .logo {
      position: absolute;
      top: 20px;
      left: 20px;
      width: 80px;
      height: 80px;
      z-index: 3;
    }

    .bg-img {
      position: absolute;
      width: 100%;
      height: 100%;
      object-fit: cover;
      top: 0;
      left: 0;
      z-index: 1;
    }

    .welcome-text {
      position: relative;
      z-index: 2;
      /*margin-left: 60px;*/
      margin-top: 580px;
      text-align: center;
      font-family: "Poppins", "Segoe UI Rounded", "Arial Rounded MT Bold",
      sans-serif;

      h1 {
        margin: 0;
        font-size: 52px;
        font-weight: 800;
        background: linear-gradient(90deg, #1e90ff, #32cd32);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        text-shadow: 2px 2px 6px rgba(0, 0, 0, 0.25);
      }

      .hi {
        margin-left: 0;
      }
      .welcome {
        margin-left: 40px;
      }
      .brand {

        font-size: 50px;
        letter-spacing: 4px;
      }
    }

    .company-name {
      position: absolute;
      bottom: 30px;
      width: 100%;
      text-align: center;
      font-size: 16px;
      font-weight: 500;
      color: #444;
      z-index: 2;
    }

    .icp {
      position: absolute;
      bottom: 10px;
      width: 100%;
      text-align: center;
      font-size: 13px;
      font-weight: 500;
      color: rgba(68, 68, 68, 0.46);
      z-index: 2;
    }
  }



  /* 右边区域 */
  .right-box {
    flex: 3;
    display: flex;
    justify-content: center;
    align-items: center;
    background: #ffffff;
    position: relative; /* 关键：作为定位容器 */

    .logo {
      position: absolute;
      top: 20px;
      right: 20px;
      width: 50px;
      height: 50px;
      z-index: 3;
    }

    .login-card {
      width: 600px;
      padding: 40px 30px;

      .title-row {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-bottom: 60px;
        .title {
          font-size: 28px;
          font-weight: 700;
          margin: 0;
          color: #333;
        }
        .title-icon {
          width: 32px;
          height: 32px;
          margin-left: 12px;
        }
      }

      .underline-input {
        margin-bottom: 60px;
        /deep/ .el-input__inner {
          border: none;
          border-bottom: 2px solid #ccc;
          border-radius: 0;
          padding: 12px 8px;
          font-size: 20px;
          &:focus {
            border-bottom: 2px solid #1e90ff;
          }
        }
      }

      .login-btn {
        margin-top: 10px;
        width: 100%;
        height: 50px;
        font-size: 18px;
        border-radius: 25px;
      }

      /* 其他登录方式 */
      .other-login {
        margin-top: 40px;

        .other-title {
          font-size: 14px;
          color: #888;
          display: block;
          margin-bottom: 15px;
          text-align: left; /* 靠左 */
        }

        .icons {
          display: flex;
          justify-content: flex-start; /* 图标靠左 */

          .icon-item {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-right: 30px;
            cursor: pointer;

            .circle {
              width: 50px;
              height: 50px;
              border-radius: 50%;
              background: #f2f2f2;
              display: flex;
              justify-content: center;
              align-items: center;
              transition: all 0.3s;

              img {
                width: 28px;
                height: 28px;
              }

              &:hover {
                background: #1e90ff;
              }
              &:hover img {
                filter: brightness(0) invert(1);
              }
            }

            p {
              margin-top: 6px;
              font-size: 14px;
              color: #555;
            }
          }
        }
      }
    }
  }
}
</style>



<script>
import {getCodeImg} from "@/api/login";
import Cookies from "js-cookie";
import {encrypt, decrypt} from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        phonenumber: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        phonenumber: [
          {required: true, trigger: "blur", message: "用户名不能为空"}
        ],
        password: [
          {required: true, trigger: "blur", message: "密码不能为空"}
        ],
        code: [{required: true, trigger: "change", message: "验证码不能为空"}]
      },
      loading: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  mounted() {

  },
  created() {
    /*this.getCode();*/
    this.getCookie();
  },
  methods: {
    handleSmsLogin(){

    },
    getCode() {
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const phonenumber = Cookies.get("phonenumber");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        phonenumber: phonenumber === undefined ? this.loginForm.phonenumber : phonenumber,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("phonenumber", this.loginForm.phonenumber, {expires: 30});
            Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
            Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
          } else {
            Cookies.remove("phonenumber");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store
            .dispatch("Login", this.loginForm)
            .then(() => {
              this.$router.push({path: this.redirect || "/"});
            })
            .catch(() => {
              this.loading = false;
              //this.getCode();
            });
        }
      });
    }
  }
};
</script>

